import Core.WindFunctions as WindFunction
import Core.Gadget as Gadget
import Core.IO as IO
import Core.Global as Global
import Core.Config as Config
import DataProcess
import DataProcess.Fundamental as Fundamental
from datetime import *

config = Config.Config()
database = config.DataBase()

#---Code Below---
#---Connect to DataBase, Find Series 连接数据库---
#database = MongoDB.MongoDB("localhost","27017")

def Statics(database):
    total = 0
    completeData = 0
    period1 = 0
    period2 = 0
    period3 = 0
    period4 = 0
    instruments = database.findWithFilter("Instruments","Stock")
    for instrument in instruments:
        symbol = instrument["Symbol"]
        statements = database.findWithFilter("Fundamental",symbol + "_Fundamental")
        total = total + len(statements)
        for statement  in statements:
            if statement.get("Wind") != None and len(statement["Wind"]) >= 151:
                completeData = completeData + 1
                if statement["Period"] == 1:
                    period1 = period1 + 1
                if statement["Period"] == 2:
                    period2 = period2 + 1
                if statement["Period"] == 3:
                    period3 = period3 + 1
                if statement["Period"] == 4:
                    period4 = period4 + 1

    print("Total Statements:" + str(total))
    print("Total Complete Data:" + str(completeData))
    print("Total Period1:" + str(period1))
    print("Total Period2:" + str(period2))
    print("Total Period3:" + str(period3))
    print("Total Period4:" + str(period4))


# ---统计指定日期的报告数量（掌握目前更新进度）---
def StatisticsTargetDate(database, reportDate):
    #strReportDate =
    target = Gadget.ToDateTimeString(reportDate)

    #
    count = 0
    instruments = Gadget.FindListedInstrument(database,reportDate)
    total = len(instruments)
    for instrument in instruments:
        if count % 100 == 0:
            print("Process", count)
        symbol = instrument["Symbol"]
        statements = database.find("Fundamental", symbol + "_Fundamental", query={"ReportDate":target})
        if len(statements) != 0:
            count += 1

    #
    print("Collected Reports " + str(count) + " " + format(count/total, '.0%') + " @ " + target)


def ReleaseDateStatistics(month,day):
    filter = {}
    instruments = database.findWithFilter("Instruments", "Stock", filter)

    statistics = {}

    filter = {}
    for instrument in instruments:
        symbol = instrument["Symbol"]
        filter["Period"] = 4
        statements = database.findWithFilter("Fundamental",symbol + "_Fundamental", filter)
        for statement in statements:
            year = int(statement["ReportDate"][:4])
            if statistics.get(year) == None:
                item = {}
                item["Pre"] = 0
                item["Post"] = 0
                statistics[year] = item

            item = statistics.get(year)
            dt = datetime(year+1,month,day)
            dt = Gadget.ToUTCDateTime(dt)
            if statement["StdDateTime"] < dt:
                item["Pre"] += 1
            else:
                item["Post"] += 1

    for k,v in statistics.items():
        print("Year: " + str(k) + " Pre: " + str(v["Pre"]) + " Post: " + str(v["Post"]))


def CheckField(database,catagory = "Wind", field = "WorkingCapital"):
    count = 0
    instruments = database.findAll("Instruments","Stock")
    for instrument in instruments:
        symbol = instrument["Symbol"]

        if symbol == "002523.SZ":
            kkwood = 0

        fundamentals = database.findAll("Fundamental",symbol + "_Fundamental")

        if fundamentals.__len__() == 0:
            #print(symbol + " NoReports")
            continue
        count = count + len(fundamentals)
        for statement in fundamentals:
            reportDate = statement["ReportDate"]

            if catagory not in statement:
                continue
            if field not in statement[catagory]:
                print(symbol + " " + reportDate + "Field:" + field + "Not include")
                #reportDateTime = Gadget.ParseDateTime(reportDate)
                #CollectReport(database,symbol,reportDateTime)
                continue
            if statement[catagory][field] > 0:

                print(symbol + " " + reportDate + " Checked Field " )
                totalEquity = statement[catagory]["tot_equity"]
                cs = statement[catagory]["cap_stk"]
                ts = statement[catagory]["tsy_stk"]
                ps = statement[catagory]["other_equity_instruments_PRE"]
                reserve1 = statement[catagory]["cap_rsrv"]
                reserve2 = statement[catagory]["surplus_rsrv"]
                reserve3 = statement[catagory]["prov_nom_risks"]
                minority = statement[catagory]["minority_int"]
                retend = statement[catagory]["undistributed_profit"]
                comprehensive = statement[catagory]["other_compreh_inc_bs"]

                kkwood = 0

    print("Total Report " + str(count))


def CheckZeroInterestEffects(instrument,reportDate):
    symbol = instrument["Symbol"]
    filter = {}
    filter["ReportDate"] = Gadget.ToDateTimeString(reportDate)
    statements = database.findWithFilter("Fundamentals",symbol + "_Fundamental",filter)
    statement = statements[0]


def CheckDuplicated(database,instruments,datetime1,datetime2):

    targetDates = GenerateReportDates(datetime1,datetime2)
    for instrument in instruments:
        symbol = instrument["Symbol"]
        for date in targetDates:
            reportDate = Gadget.ToDateTimeString(date)
            filter = {"ReportDate": reportDate}
            fundamentals = database.findWithFilter("Fundamental",symbol + "_Fundamental",filter)
            length = len(fundamentals)
            if length > 1:
                print(symbol + " "+ reportDate + " Duplicated" )
            kkwood = 0


def CheckMissingWindValues(database,instruments):
    for instrument in instruments:
        symbol = instrument["Symbol"]
        print(symbol)

        contents = []
        contents = WindFunction.FundamentalFields()
        fundamentals = database.findAll("Fundamental",symbol + "_Fundamental")
        for report in fundamentals:
            reportDate = report["ReportDate"]
            missing = 0
            for field in contents:
                if field not in report["Wind"]:
                    # print("Missing Field " + symbol + " " + reportDate + " Field " + field)
                    missing = missing + 1
            if missing > 0:
                print("Missing Field " + symbol + " " + reportDate + " Count " + str(missing))


#---2017-5-12---
#---Total Expense 包含所有（Dep && Interest Expense）---
#--- OperatingProfit1 = Total Rev - Total + Interest
#--- OperatingProfit2 (Wind opprofit) = Total Rev - Total Exp + Invest
def FixOpratingProfit1(database,instruments):
    count = 0
    for instrument in instruments:
        count = count + 1
        symbol = instrument["Symbol"]
        print(symbol + " count:" + str(count))
        collectionName = symbol + "_Fundamental"
        fundamentals = database.findAll("Fundamental",collectionName)
        if len(fundamentals) == 0:
            print(symbol + " NoReport")
            continue

        for statement in fundamentals:
            reportDate = statement["ReportDate"]
            totalRev = statement["Values"]["TotalRevenue"]
            totalExp = statement["Values"]["TotalExpense"]
            interest = statement["Values"]["InterestExpense"]
            if interest > 0:
                kkwood = 0

            dep = statement["Values"]["DepAmort"]
            operatingProfit1 = totalRev - totalExp + interest
            eitda = operatingProfit1 + dep
            target = {"ReportDate":reportDate}
            values = {}
            values["Values.OperatingProfit1"] = operatingProfit1
            values["Values.EBITDA"] = eitda
            database.update("Fundamental",collectionName,target,values)
            kkwood = 1


def FixFreeCashFlow(database,instruments):
    count = 0
    for instrument in instruments:
        count = count + 1
        symbol = instrument["Symbol"]
        print(symbol + " count:" + str(count))
        collectionName = symbol + "_Fundamental"
        fundamentals = database.findAll("Fundamental",collectionName)
        if len(fundamentals) == 0:
            print(symbol + " NoReport")
            continue

        for statement in fundamentals:
            reportDate = statement["ReportDate"]

            if "FreeCashFlow" not in statement["Values"]:
                print(symbol + " Fix FreeCashFlow @" + reportDate)
            #
            cf = statement["Values"]["OperatingCashFlow"]
            exp = statement["Values"]["CapitalExpenditure"]
            fcf = cf - exp

            target = {"ReportDate":reportDate}
            values = {}
            values["Values.FreeCashFlow"] = fcf
            database.update("Fundamental", collectionName, target, values)
            kkwood = 1

# --- 2018.4 ExpendFields ---
def ExpendFields(database,instruments):
    count = 0

    #
    for instrument in instruments:

        fields = []
        fields.append("investcapital")
        fields.append("interestdebt")
        fields.append("tangibleasset")
        fields.append("retainedearnings")
        fields.append("exinterestdebt_current")
        fields.append("exinterestdebt_noncurrent")
        #
        fields.append("r_and_d_costs")
        fields.append("deferred_inc_non_cur_liab")

        if instrument["Properties"]["Industry"] == "银行":
            fields.append("borrow_central_bank")


        count = count + 1
        symbol = instrument["Symbol"]
        print(symbol + " count:" + str(count))
        if count <=2050:
            continue
        collectionName = symbol + "_Fundamental"
        fundamentals = database.findAll("Fundamental",collectionName)
        if len(fundamentals) == 0:
            print(symbol + " NoReport")
            continue

        for statement in fundamentals:
            reportDate = statement["ReportDate"]
            print("ReportDate " + reportDate)
            reportDate = Gadget.ParseDateTime(reportDate)
            WindFunction.Request_Write_Fundamentals_Extension(database, symbol, reportDate, fields)


# --- 2018.4.24 ExpendFields ---
def ExpendFields20180424(database,instruments):
    count = 0

    #
    for instrument in instruments:

        fields = []
        fields.append("other_grants_inc")
        fields.append("gain_asset_dispositions")

        count = count + 1
        symbol = instrument["Symbol"]
        print(symbol + " count:" + str(count))
        #if count <=2050:
        #    continue
        collectionName = symbol + "_Fundamental"
        fundamentals = database.findAll("Fundamental",collectionName)
        if len(fundamentals) == 0:
            print(symbol + " NoReport")
            continue

        for statement in fundamentals:
            reportDate = statement["ReportDate"]
            print("Expend Field @ ReportDate " + reportDate)
            reportDate = Gadget.ParseDateTime(reportDate)
            WindFunction.Request_Write_Fundamentals_Extension(database, symbol, reportDate, fields)


#---强制补齐---
def FixMissingReport():
    header,table = IO.ReadCSVFile("d:/MissingWindDataReport.csv")
    for missing in table:
        symbol = missing[0]
        reportDate = Gadget.ParseDateTime(missing[1],"%Y/%m/%d")
        print("Processing:" + missing[0] + " " + missing[1])
        CollectReport(database,symbol,reportDate)
        kkwood


def MissingReportList():
    missingReports = []
    # missingReports.append({"Symbol": "000620.SZ", "ReportDate": datetime(2008, 9, 30)})
    # missingReports.append({"Symbol": "000748.SZ", "ReportDate": datetime(2016, 12, 31)})
    missingReports.append({"Symbol": "000748.SZ", "ReportDate": datetime(2017, 3, 31)})
    missingReports.append({"Symbol": "600005.SH", "ReportDate": datetime(2016, 12, 31)})
    missingReports.append({"Symbol": "600005.SH", "ReportDate": datetime(2017, 3, 31)})
    missingReports.append({"Symbol": "000522.SZ", "ReportDate": datetime(2013, 3, 31)})
    missingReports.append({"Symbol": "000562.SZ", "ReportDate": datetime(2014, 12, 31)})
    missingReports.append({"Symbol": "000602.SZ", "ReportDate": datetime(2013, 6, 30)})
    missingReports.append({"Symbol": "000618.SZ", "ReportDate": datetime(2005, 12, 31)})
    missingReports.append({"Symbol": "000699.SZ", "ReportDate": datetime(2007, 3, 31)})
    missingReports.append({"Symbol": "000763.SZ", "ReportDate": datetime(2005, 12, 31)})
    missingReports.append({"Symbol": "000765.SZ", "ReportDate": datetime(2005, 3, 31)})
    missingReports.append({"Symbol": "000765.SZ", "ReportDate": datetime(2005, 6, 30)})
    missingReports.append({"Symbol": "000817.SZ", "ReportDate": datetime(2005, 12, 31)})
    missingReports.append({"Symbol": "200041.SZ", "ReportDate": datetime(2009, 9, 30)})
    missingReports.append({"Symbol": "600002.SH", "ReportDate": datetime(2006, 3, 31)})
    missingReports.append({"Symbol": "600092.SH", "ReportDate": datetime(2005, 12, 31)})
    missingReports.append({"Symbol": "600092.SH", "ReportDate": datetime(2006, 3, 31)})
    missingReports.append({"Symbol": "600092.SH", "ReportDate": datetime(2006, 6, 30)})
    missingReports.append({"Symbol": "600092.SH", "ReportDate": datetime(2006, 9, 30)})
    missingReports.append({"Symbol": "600102.SH", "ReportDate": datetime(2011, 12, 31)})
    missingReports.append({"Symbol": "600205.SH", "ReportDate": datetime(2007, 3, 31)})
    missingReports.append({"Symbol": "600253.SH", "ReportDate": datetime(2013, 6, 30)})
    missingReports.append({"Symbol": "600263.SH", "ReportDate": datetime(2011, 12, 31)})
    missingReports.append({"Symbol": "600286.SH", "ReportDate": datetime(2006, 12, 31)})
    missingReports.append({"Symbol": "600286.SH", "ReportDate": datetime(2007, 3, 31)})
    missingReports.append({"Symbol": "600296.SH", "ReportDate": datetime(2007, 3, 31)})
    missingReports.append({"Symbol": "600553.SH", "ReportDate": datetime(2010, 12, 31)})
    missingReports.append({"Symbol": "600591.SH", "ReportDate": datetime(2009, 12, 31)})
    missingReports.append({"Symbol": "600607.SH", "ReportDate": datetime(2009, 12, 31)})
    missingReports.append({"Symbol": "600659.SH", "ReportDate": datetime(2005, 6, 30)})
    missingReports.append({"Symbol": "600659.SH", "ReportDate": datetime(2005, 9, 30)})
    missingReports.append({"Symbol": "600659.SH", "ReportDate": datetime(2005, 12, 31)})
    missingReports.append({"Symbol": "600772.SH", "ReportDate": datetime(2005, 12, 31)})
    missingReports.append({"Symbol": "600772.SH", "ReportDate": datetime(2006, 3, 31)})
    missingReports.append({"Symbol": "600772.SH", "ReportDate": datetime(2006, 6, 30)})
    missingReports.append({"Symbol": "600772.SH", "ReportDate": datetime(2006, 9, 30)})
    missingReports.append({"Symbol": "600786.SH", "ReportDate": datetime(2007, 12, 31)})
    missingReports.append({"Symbol": "600799.SH", "ReportDate": datetime(2005, 12, 31)})
    missingReports.append({"Symbol": "600842.SH", "ReportDate": datetime(2009, 12, 31)})
    missingReports.append({"Symbol": "600991.SH", "ReportDate": datetime(2011, 12, 31)})
    # missingReports.append({"Symbol": "T00018.SH", "ReportDate": datetime(2006, 9, 30)})

    return missingReports


def MissingReport20180930():
    missingReports = []
    missingReports.append({"Symbol": "002680.SZ", "ReportDate": datetime(2018, 9, 30)})
    missingReports.append({"Symbol": "601313.SH", "ReportDate": datetime(2018, 9, 30)})
    missingReports.append({"Symbol": "300694.SZ", "ReportDate": datetime(2018, 9, 30)})
    missingReports.append({"Symbol": "601162.SH", "ReportDate": datetime(2018, 9, 30)})
    missingReports.append({"Symbol": "002940.SZ", "ReportDate": datetime(2018, 9, 30)})
    missingReports.append({"Symbol": "002939.SZ", "ReportDate": datetime(2018, 9, 30)})
    return missingReports


def MissingShareHolders20180930():
    missingReports = []
    datetime2 = datetime(2018, 9, 30)
    '''
    missingReports.append({"Symbol": "	000004.SZ	", "ReportDate": datetime2})
    missingReports.append({"Symbol": "	000662.SZ	", "ReportDate": datetime2})
    missingReports.append({"Symbol": "	000978.SZ	", "ReportDate": datetime2})
    missingReports.append({"Symbol": "	002492.SZ	", "ReportDate": datetime2})
    missingReports.append({"Symbol": "	002673.SZ	", "ReportDate": datetime2})
    missingReports.append({"Symbol": "	002709.SZ	", "ReportDate": datetime2})
    missingReports.append({"Symbol": "	002769.SZ	", "ReportDate": datetime2})
    missingReports.append({"Symbol": "	002786.SZ	", "ReportDate": datetime2})
    missingReports.append({"Symbol": "	300022.SZ	", "ReportDate": datetime2})
    missingReports.append({"Symbol": "	300071.SZ	", "ReportDate": datetime2})
    missingReports.append({"Symbol": "	300093.SZ	", "ReportDate": datetime2})
    missingReports.append({"Symbol": "	300166.SZ	", "ReportDate": datetime2})
    missingReports.append({"Symbol": "	300180.SZ	", "ReportDate": datetime2})
    missingReports.append({"Symbol": "	300265.SZ	", "ReportDate": datetime2})
    missingReports.append({"Symbol": "	300486.SZ	", "ReportDate": datetime2})
    missingReports.append({"Symbol": "	600775.SH	", "ReportDate": datetime2})
    missingReports.append({"Symbol": "	600818.SH	", "ReportDate": datetime2})
    missingReports.append({"Symbol": "	600835.SH	", "ReportDate": datetime2})
    missingReports.append({"Symbol": "	601929.SH	", "ReportDate": datetime2})
    missingReports.append({"Symbol": "	603116.SH	", "ReportDate": datetime2})
    missingReports.append({"Symbol": "	300515.SZ	", "ReportDate": datetime2})
    missingReports.append({"Symbol": "	002808.SZ	", "ReportDate": datetime2})
    missingReports.append({"Symbol": "	300581.SZ	", "ReportDate": datetime2})
    missingReports.append({"Symbol": "	002862.SZ	", "ReportDate": datetime2})
    missingReports.append({"Symbol": "	601326.SH	", "ReportDate": datetime2})
    missingReports.append({"Symbol": "	300719.SZ	", "ReportDate": datetime2})
    missingReports.append({"Symbol": "	603106.SH	", "ReportDate": datetime2})
    missingReports.append({"Symbol": "	000096.SZ	", "ReportDate": datetime2})
    missingReports.append({"Symbol": "	000407.SZ	", "ReportDate": datetime2})
    missingReports.append({"Symbol": "	000540.SZ	", "ReportDate": datetime2})
    missingReports.append({"Symbol": "	000597.SZ	", "ReportDate": datetime2})
    missingReports.append({"Symbol": "	000600.SZ	", "ReportDate": datetime2})
    missingReports.append({"Symbol": "	000690.SZ	", "ReportDate": datetime2})
    missingReports.append({"Symbol": "	000725.SZ	", "ReportDate": datetime2})
    missingReports.append({"Symbol": "	000889.SZ	", "ReportDate": datetime2})
    missingReports.append({"Symbol": "	002165.SZ	", "ReportDate": datetime2})
    missingReports.append({"Symbol": "	002339.SZ	", "ReportDate": datetime2})
    missingReports.append({"Symbol": "	002349.SZ	", "ReportDate": datetime2})
    missingReports.append({"Symbol": "	002559.SZ	", "ReportDate": datetime2})
    missingReports.append({"Symbol": "	002586.SZ	", "ReportDate": datetime2})
    missingReports.append({"Symbol": "	300009.SZ	", "ReportDate": datetime2})
    missingReports.append({"Symbol": "	300026.SZ	", "ReportDate": datetime2})
    missingReports.append({"Symbol": "	300035.SZ	", "ReportDate": datetime2})
    missingReports.append({"Symbol": "	300075.SZ	", "ReportDate": datetime2})
    missingReports.append({"Symbol": "	300163.SZ	", "ReportDate": datetime2})
    missingReports.append({"Symbol": "	300307.SZ	", "ReportDate": datetime2})
    missingReports.append({"Symbol": "	603268.SH	", "ReportDate": datetime2})
    missingReports.append({"Symbol": "	603286.SH	", "ReportDate": datetime2})
    missingReports.append({"Symbol": "	603383.SH	", "ReportDate": datetime2})
    '''
    missingReports.append({"Symbol": "	300760.SZ	", "ReportDate": datetime2}) #没有更新成功

    for m in missingReports:
        m["Symbol"] = m["Symbol"].strip()
    return missingReports


def Download_MissingReport(database, missingReports):

    for missingReport in missingReports:
        symbol = missingReport["Symbol"]
        reportDate = missingReport["ReportDate"]
        print("Try to Download Missing Report", symbol, reportDate)
        Fundamental.Request_Write_Fundamentals(database, symbol, reportDate)
        kkwood = 0


def Download_MissingShareHolders(database, missingReports):

    for missingReport in missingReports:
        symbol = missingReport["Symbol"]
        reportDate = missingReport["ReportDate"]
        print("Try to Download Missing ShareHolders", symbol, reportDate)
        Fundamental.Request_Write_Fundamentals_SharesHolder_Combo(database, symbol, reportDate)
        kkwood = 0
    pass

def FixUnset(database,instruments):
    for instrument in instruments:
        symbol = instrument["Symbol"]
        print(symbol)
        fundamentals = database.findAll("Fundamental",symbol + "_Fundamental")
        coll =  database.client["Fundamental"][symbol + "_Fundamental"]
        for fund in fundamentals:
            reportDate = fund["ReportDate"]
            if "Values" in fund:
                update = {}
                update["$unset"] = {}
                update["$unset"]["Values.FCF"] = ""
                update["$unset"]["Values.InvstCashFlow"] = ""
                update["$unset"]["Values.PriceEarning"] = ""
                update["$unset"]["Values.PriceSale"] = ""
                update["$unset"]["Values.EPS_ttm"] = ""
                update["$unset"]["Values.Recivable"] = ""
                coll.update_one({"ReportDate":reportDate}, update, False)


def FixMissingField(database, field, windField):

    instruments = database.findWithFilter("Instruments","Stock")
    count = 0
    start = False
    for instrument in instruments:
        count = count + 1
        symbol = instrument["Symbol"]

        if symbol == "600005.SH":
            start = True
        if start == False:
            continue


        print("Count " + symbol + " " + str(count))
        collectionName = symbol + "_Fundamental"
        fundamentals = database.findAll("Fundamental",collectionName)
        if len(fundamentals) == 0:
            #print(symbol + " NoReport")
            continue

        #

        for statement in fundamentals:
            if field not in statement["Values"]:
                #
                rptDateStr = statement["ReportDate"]
                rptDateStr = rptDateStr[:10]
                params = {}
                params["rptDate"] = rptDateStr
                #params["endDate"] = rptDateStr
                params["tradeDate"] = rptDateStr
                #params["currencyType"] = ""
                params["rptType"] = "1"
                #params["priceAdj"] = "U"
                #params["cycle"] = "D"
                #
                values = WindFunction.WSS(symbol, [windField], params)
                print("Missing " + symbol + " " + rptDateStr + "  " + field + " FilledBy ")
                kkwood = 0


def FixZeroField(database, field, windField):
    instruments = database.findWithFilter("Instruments", "Stock")
    count = 0
    start = False
    for instrument in instruments:
        count = count + 1
        symbol = instrument["Symbol"]

        #if symbol == "600005.SH":
        #    start = True
        #if start == False:
        #    continue

        print("Count " + symbol + " " + str(count))
        collectionName = symbol + "_Fundamental"
        fundamentals = database.findAll("Fundamental", collectionName)
        if len(fundamentals) == 0:
            # print(symbol + " NoReport")
            continue

        #
        for statement in fundamentals:
            if field in statement["Values"] and statement["Values"][field] == 0:
                #
                rptDateStr = statement["ReportDate"]
                rptDateStr = rptDateStr[:10]
                params = {}
                params["rptDate"] = rptDateStr
                # params["endDate"] = rptDateStr
                params["tradeDate"] = rptDateStr
                # params["currencyType"] = ""
                params["rptType"] = "1"
                # params["priceAdj"] = "U"
                # params["cycle"] = "D"
                #
                values = WindFunction.WSS(symbol, [windField], params)
                print("Zero " + symbol + " " + rptDateStr + "  " + field + " FilledBy ")
                kkwood = 0


def CollectTotalHoldersInfo(database):
    kkwood = 0


def ManuallyAddTop10SharesHolder(database, pathfilename, symbol, reportDate):

    holders = []
    header,data = IO.ReadExcelFile(pathfilename)
    for d in data:
        entry = {}
        entry["HolderName"] = d[header["股东名称"]]
        entry["Amount"] = d[header["持股数量(股)"]]
        entry["Ratio"] = d[header["占总股本比例(%)"]]
        entry["ShareType"] = d[header["股本性质"]]
        holders.append(entry)

    dataObject = {}
    dataObject["ShareHolders"] = holders

    # ---Upsert---
    target = {}
    target["ReportDate"] = Gadget.ToDateTimeString(reportDate)
    target["Symbol"] = symbol
    database.upsert("Fundamental", symbol + "_Fundamental", target, dataObject)


def ManuallyAddTop10FreeFloatSharesHolder(database, pathfilename, symbol, reportDate):

    holders = []
    header,data = IO.ReadExcelFile(pathfilename)
    for d in data:
        entry = {}
        entry["HolderName"] = d[header["股东名称"]]
        entry["Amount"] = d[header["持股数量(股)"]]
        entry["Ratio"] = d[header["占总股本比例(%)"]]
        holders.append(entry)

    dataObject = {}
    dataObject["FreeFloatShareHolders"] = holders

    # ---Upsert---
    target = {}
    target["ReportDate"] = Gadget.ToDateTimeString(reportDate)
    target["Symbol"] = symbol
    database.upsert("Fundamental", symbol + "_Fundamental", target, dataObject)


def Download_Top10ShareHolders(database):
    #
    instruments = database.findWithFilter("Instruments", "Stock")
    count = 0
    #
    for instrument in instruments:
        count = count + 1
        symbol = instrument["Symbol"]

        #if count < 300:
        #    continue
        #000831.SZ

        print("Count " + symbol + " " + str(count))
        collectionName = symbol + "_Fundamental"
        fundamentals = database.findAll("Fundamental", collectionName)
        if len(fundamentals) == 0:
            # print(symbol + " NoReport")
            continue

        #
        for statement in fundamentals:
            if "ShareHolders" not in statement:
                #
                reportDateStr = statement["ReportDate"]
                reportDate = Gadget.ParseDateTime(reportDateStr)
                #
                Fundamental.Request_Write_Fundamentals_SharesHolder(database, symbol, reportDate,
                                                        command="top10liqshareholders",
                                                        fields="name,amount,ratio",
                                                        callBack=Fundamental.CallBackTopFreeFloatSharesHolder)

                Fundamental.Request_Write_Fundamentals_SharesHolder(database, symbol, reportDate,
                                                        command="top10shareholders",
                                                        fields="name,amount,ratio,type",
                                                        callBack=Fundamental.CallBackTopSharesHolder)
                #
                print("Fixed Top10Shares " + symbol + " " + reportDateStr)
                kkwood = 0




# ---统计指定日期的报告---
datetime2 = datetime(2018,9,30)
#StatisticsTargetDate(database, datetime2)
#ReleaseDateStatistics(4,30)
#Statics(database)
k = 0

#
WindFunction.w.start() # 启动Wind API

# ---扩展字段---
# ExpendFields(database, instruments)
# ExpendFields20180424(database, instruments)


# ---补齐数据---
# Download_Top10ShareHolders(database)
# Download_MissingReport(database)
missing = MissingShareHolders20180930()
Download_MissingShareHolders(database, missing)
# Fundamental.Download_Fundamental(database, "600005.SH", datetime(2016,12,31))
k = 2

# ---获取季度财报---
# datetime1 = datetime(2017,6,1)
# datetime2 = datetime(2017,11,1)
# reportDates = Gadget.GenerateReportDates(datetime1, datetime2)
# 以下兩種方式基本等效
# DataProcess.Download_Data(database, instruments, reportDates, DataProcess.Download_Fundamental)
# DataProcess.Download_Fundamentals(database, instruments, datetime1, datetime2)

#---Collect Industry Indormation---
# releaseDateTimes = Gadget.GenerateReleaseDates(datetime(2000,1,1),datetime(2017,6,1))
# DataProcess.Download_Data(database, instruments,releaseDateTimes, DataProcess.CollectIndustry)

# ManuallyAddTop10SharesHolder(database,"D:\Data\FundamentalLog/000786SZTop10_2017-10-09.xlsx","000786.SZ",datetime(2017,9,30))
# ManuallyAddTop10SharesHolder(database,"D:\Data\FundamentalLog/603169SH_Top10_20171009.xlsx","603169.SH",datetime(2017,9,30))

#---菲规律信息---
#WindFunctions.Request_Write_RestrictShares(database,datetime2)


# ---检查数据---
instruments = []
filter = {}
#filter["Symbol"] = "000768.SZ"
#ipoDate = datetime(2005,1,1)
#filter["IPO_Date"] =  {"$gt": ipoDate}
instruments = database.findWithFilter("Instruments","Stock",filter)

datetime1 = datetime(2018,9,30)
datetime2 = datetime(2018,9,30)
# missingReports = Fundamental.CheckMissingReport(database,instruments,datetime1,datetime2)

# CheckDuplicated(database,instruments,datetime1,datetime2)
# CheckField(database,field="tsy_stk")
# CheckMissingWindValues(database, instruments)

Fundamental.CheckMissingShareHolders(database, instruments, datetime1, datetime2)
k = 1

# ---修正数据---
# FixFreeCashFlow(database, [{"Symbol":"000333.SZ"}])
# FixFreeCashFlow(database, [{"Symbol":"600823.SH"}])
# Fundamental.FormatingReports(database, instruments)

# ---Fixing and Cleaning---
# FixOpratingProfit1(database,instruments)
# FixUnset(database,instruments)
# FormatingReport(database,instruments)
# FixMissingField(database, "TotalEquity", "tot_equity")
# FixZeroField(database, "TotalEquity", "tot_equity")
# FixZeroField(database, "TotalShares", "total_shares")

# ---精确修复---
# symbol = "002821.SZ"
# reportDate = datetime(2017,12,31)
# Fundamental.Request_Write_Fundamentals_Dividend(database, symbol, reportDate)